package homework;

import java.util.*;

public class Work10302001 {


    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);


        System.out.println("请输入查看的题目标题：");
        System.out.println("1----定义一个方法 listTest");
        System.out.println("2----迭代器和增强 for 循环打印");
        System.out.println("3----Comparable和Comparator排序");
        int i = input.nextInt();

        if( i == 1){
            testListTest();
        }else if( i == 2){
            testForAndIterator();
        }else  if( i == 3){
            testComparableAndComparator();
        }else{
            System.out.println("输入有误 -- 程序停止！");

        }


    }
//    分别用Comparable和Comparator两个接口对下列四位同学的成绩做降
//    序排序，如果成绩一样，那在成绩排序的基础上按照年龄由小到大排序
    private static void testComparableAndComparator() {
        ArrayList<StudentT> stuSet = new ArrayList<>();
        stuSet.add(new StudentT("贾宝玉",14,88.5));
        stuSet.add(new StudentT("林黛玉",13,90.5));
        stuSet.add(new StudentT("史湘云",13,85));
        stuSet.add(new StudentT("薛宝钗",15,91));

        Collections.sort(stuSet,new ComparatorTest());

        for (StudentT stu:
             stuSet) {
            System.out.println("Comparator第二种写法====="+stu.toString());
        }



    }

    static class ComparatorTest implements Comparator<StudentT>{

        @Override
        public int compare(StudentT o1, StudentT o2) {
            if(o1.age > o2.age){
                return 1;
            }
            if(o1.age < o2.age){
                return -1;
            }
            return 0;
        }
    }

//    已知数组存放一批 QQ 号码,长度 5-11 位，
//    String[] strs = {"10001","10086","12347806666","45612378901","10001","12347806666"}。
//    将该数组里面的所有 qq 号都存放在 LinkedList 中，将 list 中重复元素删
//    除，将 list 中所有元素分别用迭代器和增强 for 循环打印出来。
    private static void testForAndIterator() {
        LinkedList<String> Llist = new LinkedList<>();
        String[] strs = {"10001","10086","12347806666","45612378901","10001","12347806666"};
        for(int i = 0; i < strs.length; i++){
            Llist.add(strs[i]);
        }

        //删除重复元素
        HashSet<String> set = new HashSet<>();
        for (String newData:
             Llist) {
            System.out.println("在linkedlist中数据是"+newData);
            set.add(newData);
        }

        //foreach遍历打印
        for (String setNew:
             set ) {
            System.out.println("foreach循环打印结果"+setNew);
        }


        //Iterator 打印
        Iterator<String> iterator = set.iterator();
        //死循环  判断下一个指针的位置有没有数据
        while (iterator.hasNext()){
            System.out.println("iterator打印结果是"+iterator.next());
        }



    }

//    定义一个方法 listTest(ArrayList<String> list, String name)，要求返回
//    name 在 list 里面第一次出现的索引，如果 name 没出现过返回-1。
    private static void testListTest() {
        ArrayList<String> li = new ArrayList<>();
        li.add("阿巴阿巴");
        li.add("阿巴阿巴1");
        li.add("阿巴阿巴2");
        li.add("阿巴阿巴3");
        li.add("阿巴阿巴4");
        li.add("阿巴阿巴5");
        String name = "阿巴阿巴";

        String returnData = listTest(li,  name);
        System.out.println("查询"+name+"后查到的结果是"+returnData);

        System.out.println("重新启动系统选择题目！！！！！！！");
        
    }

    private static String listTest(ArrayList<String> li, String name) {

        for (String nameNew :
             li) {
            if(nameNew.equals(name)){
                return nameNew;
            }
        }
        return "-1";
    }

    static class StudentT{

        private String name;

        private int age;

        private double fen;


        StudentT(String name,int age,double fen){
            this.name = name;
            this.age = age;
            this.fen = fen;
        }
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public double getFen() {
            return fen;
        }

        public void setFen(double fen) {
            this.fen = fen;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", fen=" + fen +
                    '}';
        }



    }

    static class Student implements Comparable<Student>{
        private String name;
        private int age;
        private double fen;

        @Override
        public int compareTo(Student o) {

            if(this.age < o.age){
                return -1;
            }
            if(this.age == o.age){
                return 0;
            }

            return 1;
        }

        Student(String name,int age,double fen){
            this.name = name;
            this.age = age;
            this.fen = fen;
        }
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public double getFen() {
            return fen;
        }

        public void setFen(double fen) {
            this.fen = fen;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", fen=" + fen +
                    '}';
        }


    }
}
